package com.davies.learn.jbv.controller;

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;

/**
 * @author davies
 * @date 2023/8/15 10:02
 */
@RestController
public class Java11HttpClientController {


    @Autowired
    private HttpClient httpClient;


    @SneakyThrows
    @GetMapping("date")
    public CompletableFuture<String> getDate(){

        HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://localhost:3000/api/user"))
                .POST(HttpRequest.BodyPublishers.ofString("hello"))
                .build();
       return  this.httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString())
                .thenApply(HttpResponse::body);
    }

}
